package com.wjw.redis.stream.config;

import com.wjw.redis.stream.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.stream.StreamListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author ：wjw
 * &#064;date  ：Created in 2022/5/19 10:15
 * &#064;description：消费监听
 */
@Slf4j
@Component
public class ConsumeListener implements StreamListener<String, MapRecord<String, String, String>> {

    private final RedisUtil redisUtil;

    public ConsumeListener(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    @Override
    public void onMessage(MapRecord<String, String, String> message) {
        String stream = message.getStream();
        RecordId id = message.getId();
        Map<String, String> map = message.getValue();
        log.info("consumeListener[自动] group:[group-b] 接收到一个消息 stream:[{}],id:[{}],value:[{}]", stream, id, map);
//        try {
//            long result = redisUtil.del(stream, id.getValue());
//            // 使用debug级别记录操作结果，避免info日志过多
//            log.debug("consumeListener 删除{},结果：{}", id, result);
//        } catch (Exception e) {
//            // 记录异常信息，但不终止程序执行
//            log.error("处理消息时发生异常：", e);
//        }
    }

}
